package com.view.userMenu;

import com.run.BookRun;
import com.run.UserRun;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Objects;

public class returnBookMenu {
    private JLabel book_idLabel = new JLabel("图书编号");
    private JLabel book_nameLabel = new JLabel("图书名称");
    private JTextField book_idFiled = new JTextField(20);
    private JTextField book_nameFiled = new JTextField(20);
    private JButton button = new JButton("确定");
    private Box book_idBox = Box.createHorizontalBox();
    private Box book_nameBox = Box.createHorizontalBox();
    private Box mainBox = Box.createVerticalBox();

    public JPanel init(String username) {
        JPanel mainPanel = new JPanel();

        book_idBox.add(book_idLabel);
        book_idBox.add(Box.createHorizontalStrut(20));
        book_idBox.add(book_idFiled);

        book_nameBox.add(book_nameLabel);
        book_nameBox.add(Box.createHorizontalStrut(20));
        book_nameBox.add(book_nameFiled);

        mainBox.add(Box.createVerticalStrut(100));
        mainBox.add(book_idBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(book_nameBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(button);

        mainPanel.add(mainBox);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String book_id = book_idFiled.getText();
                String book_name = book_nameFiled.getText();
                String stu_id = null;
                ResultSet RESULTSET = null;
                Timestamp timestamp = new Timestamp(System.currentTimeMillis());
                String outdate = timestamp.toString();
                outdate = outdate.substring(0,10);

                Connection conn = null;
                try {
                    conn = com.util.DBUtil.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                if(Objects.equals(book_id,"")) {
                    try {
                        ResultSet resultSet = new BookRun().searchBook(conn,null,book_name);
                        while(resultSet.next()) {
                            book_id = resultSet.getString(1);
                        }

                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }

                try {
                    ResultSet resultSet = new UserRun().searchStudent(conn,username);
                    while(resultSet.next()) {
                       stu_id = resultSet.getString(1);
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

                try {
                    Boolean isReturned = false;
                    isReturned = new BookRun().returnBook(conn,stu_id,book_id,outdate);
                    if(isReturned == true) {
                        JOptionPane.showMessageDialog(null,"还书成功");
                    }
                    else {
                        JOptionPane.showMessageDialog(null,"还书失败,请检查所填信息");
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

            }
        });

        return mainPanel;

    }
}
